home *** CD-ROM | disk | FTP | other *** search
/ Champak 106 / Vol 106.iso / games / hulk.swf / scripts / com / efnx / fps / FpsBox.as
Encoding:
Text File  |  2010-04-12  |  2.2 KB  |  80 lines

  1. package com.efnx.fps
  2. {
  3.    import flash.display.Stage;
  4.    import flash.events.Event;
  5.    import flash.events.TimerEvent;
  6.    import flash.text.TextField;
  7.    import flash.text.TextFieldAutoSize;
  8.    import flash.text.TextFormat;
  9.    import flash.utils.Timer;
  10.    
  11.    public class FpsBox extends TextField
  12.    {
  13.        
  14.       
  15.       protected var format:TextFormat;
  16.       
  17.       protected var averageArray:Array;
  18.       
  19.       protected var targetFPS:int = 0;
  20.       
  21.       protected var frames:uint = 0;
  22.       
  23.       public function FpsBox(... rest)
  24.       {
  25.          var _loc2_:Timer = null;
  26.          frames = 0;
  27.          format = new TextFormat();
  28.          averageArray = new Array();
  29.          targetFPS = 0;
  30.          super();
  31.          _loc2_ = new Timer(1000);
  32.          format.font = "Verdana";
  33.          format.color = 0;
  34.          format.size = 10;
  35.          this.autoSize = TextFieldAutoSize.LEFT;
  36.          this.defaultTextFormat = format;
  37.          this.text = "-- FPS ---- AV";
  38.          _loc2_.addEventListener(TimerEvent.TIMER,tick);
  39.          if(rest[0] is Stage)
  40.          {
  41.             rest[0].addEventListener(Event.ENTER_FRAME,everyFrame,false,0,true);
  42.             targetFPS = rest[0].frameRate;
  43.          }
  44.          else
  45.          {
  46.             this.addEventListener(Event.ENTER_FRAME,everyFrame,false,0,true);
  47.          }
  48.          _loc2_.start();
  49.       }
  50.       
  51.       public function everyFrame(param1:Event) : void
  52.       {
  53.          ++frames;
  54.       }
  55.       
  56.       protected function tick(param1:TimerEvent) : void
  57.       {
  58.          var _loc2_:int = 0;
  59.          averageArray.push(frames);
  60.          if(averageArray.length == 4)
  61.          {
  62.             _loc2_ = 1;
  63.             while(_loc2_ < averageArray.length)
  64.             {
  65.                averageArray[0] += averageArray[_loc2_];
  66.                _loc2_++;
  67.             }
  68.             averageArray.splice(1,averageArray.length - 1);
  69.             averageArray[0] /= 4;
  70.          }
  71.          this.text = frames + " FPS " + Math.round(averageArray[0]) + " AV";
  72.          if(targetFPS != 0)
  73.          {
  74.             this.appendText(" /" + targetFPS);
  75.          }
  76.          frames = 0;
  77.       }
  78.    }
  79. }
  80.